//
//  XYProcessViewController.m
//  NemoSDKDemo
//
//  Created by 高超 on 2021/5/20.
//  Copyright © 2021 JackYang. All rights reserved.
//

#import "XYProcessViewController.h"
#import "XYTableViewModel.h"
#import "XYProcessCell.h"
#define IS_PRD 1
@interface XYProcessViewController ()
@property (nonatomic, strong) UITableView *proTableView;
@property (nonatomic, strong) XYTableViewModel *viewModel;

@end

@implementation XYProcessViewController

- (void)dealloc {
    NSLog(@"%s",__FUNCTION__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self initSubviews];
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTap)]];
}

- (void)viewTap {
    [self.view endEditing:YES];
}

- (void)initSubviews {
    __weak typeof(self)weakSelf = self;
    self.viewModel = [[XYTableViewModel alloc] init];
    self.proTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.proTableView.delegate = self.viewModel;
    self.proTableView.dataSource = self.viewModel;
    self.proTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.proTableView];
    
    [self.viewModel.sectionModelArray removeAllObjects];
    XYTableViewSectionModel *sectionModel = [[XYTableViewSectionModel alloc] init];
    sectionModel.headerHeight = 50;
    sectionModel.headerView = [self headerView];
    
    XYTableViewCellModel *cellModel = [[XYTableViewCellModel alloc] init];
    cellModel.height = 80.f;
    cellModel.renderBlock = ^UITableViewCell *(NSIndexPath *indexPath, UITableView *tableView) {
        XYProcessCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XYProcessCell"];
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"XYProcessCell" owner:nil options:nil].firstObject;
        }
        if (weakSelf.processType == 1) {
            cell.titleLabel.text = @"企业ID:";
            cell.inputTF.placeholder = @"请输入企业ID";
            cell.inputTF.text = IS_PRD?@"828b3c8db7050bc03b99b9fca056b39590416842":@"34d61617a620731ca655de27de12425af8a2e627";
        } else if (weakSelf.processType == 2) {
            cell.titleLabel.text = @"小鱼账号:";
            cell.inputTF.placeholder = @"请输入小鱼账号";
        } else if (weakSelf.processType == 4) {
            cell.titleLabel.text = @"会议号:";
            cell.inputTF.placeholder = @"请输入会议号";
        } else {
            cell.titleLabel.text = @"账号:";
            cell.inputTF.placeholder = @"请输入账号（三方用户唯一id）";
        }
        return cell;
    };
    [sectionModel.cellModelArray addObject:cellModel];
    
    cellModel = [[XYTableViewCellModel alloc] init];
    cellModel.height = 80.f;
    cellModel.renderBlock = ^UITableViewCell *(NSIndexPath *indexPath, UITableView *tableView) {
        XYProcessCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XYProcessCell"];
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"XYProcessCell" owner:nil options:nil].firstObject;
        }
        if (weakSelf.processType == 1) {
            cell.titleLabel.text = @"ClientID:";
            cell.inputTF.placeholder = @"请输入ClientID";
            cell.inputTF.text = IS_PRD?@"fkdH8yELVLGct8R8E2ep2lDQ":@"ZCfdE3xn0dULaGNQbOoHqDE7";
        } else if (weakSelf.processType == 2) {
            cell.titleLabel.text = @"密码:";
            cell.inputTF.placeholder = @"请输入密码";
        } else if (weakSelf.processType == 4) {
            cell.titleLabel.text = @"会议密码:";
            cell.inputTF.placeholder = @"请输入密码";
        } else {
            cell.titleLabel.text = @"昵称:";
            cell.inputTF.placeholder = @"请输入昵称";
        }
        
        return cell;
    };
    [sectionModel.cellModelArray addObject:cellModel];
    
    if (self.processType == 1) {
        cellModel = [[XYTableViewCellModel alloc] init];
        cellModel.height = 80.f;
        cellModel.renderBlock = ^UITableViewCell *(NSIndexPath *indexPath, UITableView *tableView) {
            XYProcessCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XYProcessCell"];
            if (!cell) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"XYProcessCell" owner:nil options:nil].firstObject;
            }
            cell.titleLabel.text = @"ClientSecret:";
            cell.inputTF.placeholder = @"请输入ClientSecret";
            cell.inputTF.text = IS_PRD?@"gH7uralMLHA44r3sRHuXzq8f8Lz7L9zU":@"n7R90KPyNGJybBi4QxN6fa1GiKO1OVIv";
            return cell;
        };
        [sectionModel.cellModelArray addObject:cellModel];
        
        cellModel = [[XYTableViewCellModel alloc] init];
        cellModel.height = 80.f;
        cellModel.renderBlock = ^UITableViewCell *(NSIndexPath *indexPath, UITableView *tableView) {
            XYProcessCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XYProcessCell"];
            if (!cell) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"XYProcessCell" owner:nil options:nil].firstObject;
            }
            cell.titleLabel.text = @"服务器地址:";
            cell.inputTF.placeholder = @"请输入服务器地址(address:httpsport)";
            cell.inputTF.text = IS_PRD?@"sdkapi.xylink.com:443":@"txdev-sdkapi.xylink.com:443";
            return cell;
        };
        [sectionModel.cellModelArray addObject:cellModel];
    }
    
    cellModel = [[XYTableViewCellModel alloc] init];
    cellModel.height = 80.f;
    cellModel.renderBlock = ^UITableViewCell *(NSIndexPath *indexPath, UITableView *tableView) {
        XYProcessCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XYProcessCell"];
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"XYProcessCell" owner:nil options:nil].firstObject;
        }
        cell.cellType = 1;
        if (weakSelf.processType == 1) {
            [cell.button setTitle:@"初始化" forState:UIControlStateNormal];
        } else if (weakSelf.processType == 4) {
            [cell.button setTitle:@"加入会议" forState:UIControlStateNormal];
        } else {
            [cell.button setTitle:@"登录" forState:UIControlStateNormal];
        }
        cell.action = ^{
            [weakSelf buttonClick];
        };
        return cell;
    };
    [sectionModel.cellModelArray addObject:cellModel];
    
    [self.viewModel.sectionModelArray addObject:sectionModel];
    
    
    UILabel *tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, kScreen.height-150, kScreen.width-60, 80)];
    tipsLabel.font = [UIFont systemFontOfSize:14];
    tipsLabel.numberOfLines = 0;
    tipsLabel.textAlignment = NSTextAlignmentCenter;
    if (self.processType == 1) {
        tipsLabel.text = @"注意：以上参数为小鱼提供的测试数据，仅供演示和测试使用，不会保存数据，为了不影响您正常使用，请您联系小鱼商务获取正式参数";
        [self.view addSubview:tipsLabel];
    }
    if (self.processType == 2) {
        tipsLabel.text = @"如果您还没有小鱼账号，可下载小鱼易连App注册账号";
        [self.view addSubview:tipsLabel];
        UIButton *downloadBuuton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        downloadBuuton.frame = CGRectMake((kScreen.width-80)/2, CGRectGetMaxY(tipsLabel.frame)-20, 80, 50);
        [downloadBuuton setTitle:@"去下载 >>" forState:UIControlStateNormal];
        [downloadBuuton addTarget:self action:@selector(downloadXYLinkApp) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:downloadBuuton];
    }
}

- (UIView *)headerView {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreen.width, 50)];
    if (self.processType == 1) {
        label.text = @"初始化";
    } else if (self.processType == 2) {
        label.text = @"小鱼账号登录";
    } else if (self.processType == 3) {
        label.text = @"三方账号登录";
    } else {
        label.text = @"加入会议";
    }
    label.backgroundColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:24 weight:UIFontWeightMedium];
    return label;
}


#pragma mark -Action
- (void)buttonClick {
    if (self.processType == 1) {
        XYProcessCell *eIDCell = [self.proTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        XYProcessCell *cIDCell = [self.proTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        XYProcessCell *cSeCell = [self.proTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
        XYProcessCell *serverCell = [self.proTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
        if (!eIDCell.inputTF.text.length) {
            [XYHUDManager showBriefToast:@"初始化失败，企业id不能为空"];
            return;
        }
        [[XYSDKManager share] createSDK:@"3e816492058911e7a31d000c29971af5" clientID:@"" ClientSecret:@"" serverAddress:@"39.172.69.238:7443"];
        //初始化无耗时操作
        [XYHUDManager showBriefToast:@"初始化完成"];
    }
    if (self.processType == 2) {
        XYProcessCell *accCell = [self.proTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        XYProcessCell *pwCell = [self.proTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        [[XYSDKManager share] xylinkLogin:@"18395997650" password:@"997650"];
    }
    if (self.processType == 3) {
        XYProcessCell *accCell = [self.proTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        XYProcessCell *unCell = [self.proTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        [[XYSDKManager share] login:accCell.inputTF.text userName:unCell.inputTF.text enterpriseID:[XYSettings settings].extID];
    }
    if (self.processType == 4) {
        XYProcessCell *meetingCell = [self.proTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        XYProcessCell *pwCell = [self.proTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        [[XYSDKManager share] joinMeeting:meetingCell.inputTF.text password:pwCell.inputTF.text];
    }
}

- (void)downloadXYLinkApp {
    NSString *url = @"https://www.xylink.com/download";
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

@end
